<template>
  <div class="device">
    <el-form :inline="true" :model="params" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="params.name"
          placeholder="请选择设备关键字"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="params.engineer_id" placeholder="请选择工程">
          <el-option
            :value="item.id"
            :label="item.name"
            v-for="(item, index) in engineer"
            :key="index"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="params.bind_status" placeholder="请选择绑定状态">
          <el-option value="1" label="已绑定"></el-option>
          <el-option value="0" label="未绑定"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="params.online_status" placeholder="请选择在线状态">
          <el-option value="1" label="在线"></el-option>
          <el-option value="0" label="离线"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="toSubmit">查询</el-button>
      </el-form-item>
      <el-button
        type="primary"
        size="small"
        @click="toAddClick"
        style="float: right"
        >添加</el-button
      >
    </el-form>

    <div class="table">
      <el-table :data="resData.list" stripe style="width: 100%">
        <el-table-column
          prop="serial_number"
          label="序号"
          width="60"
          type="index"
        >
        </el-table-column>
        <el-table-column prop="serial_number" label="设备编号" width="100">
        </el-table-column>
        <el-table-column prop="name" label="设备名称" align="center">
        </el-table-column>
        <el-table-column
          prop="bind_status"
          label="绑定状态"
          align="center"
          width="100"
        >
          <template v-slot="scope">
            <el-tag
              type="success"
              v-if="scope.row.bind_status === 1"
              size="mini"
              >已绑定</el-tag
            >
            <el-tag type="danger" v-else size="mini">未绑定</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="online_status"
          label="在线状态"
          width="100"
          align="center"
        >
          <template v-slot="scope">
            <el-tag
              type="success"
              v-if="scope.row.online_status === 1"
              size="mini"
              >在线</el-tag
            >
            <el-tag type="danger" v-else size="mini">离线</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="longitude"
          label="经度"
          width="200"
          align="center"
        >
          <template v-slot="scope">
            <el-tag type="info" v-if="scope.row.longitude === null" size="mini"
              >暂未绑定</el-tag
            >
            <span v-else>{{ scope.row.longitude }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="latitude"
          label="纬度"
          width="200"
          align="center"
        >
          <template v-slot="scope">
            <el-tag type="info" v-if="scope.row.latitude === null" size="mini"
              >暂未绑定</el-tag
            >
            <span v-else>{{ scope.row.latitude }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="video" label="视频" width="150" align="center">
          <template v-slot="scope">
            <el-tag type="info" v-if="scope.row.video === null" size="mini"
              >暂未开通</el-tag
            >
            <span v-else>{{ scope.row.video }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template v-slot="{ row }">
            <el-button type="text" @click="toEdit(row)">编辑</el-button>
            <el-button type="text" @click="toRemove(row.id)" style="color: red"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      @close="closeDialog"
    >
      <el-form :model="form" :rules="rules" ref="ruleForm">
        <el-form-item
          label="设备编号"
          :label-width="formLabelWidth"
          prop="serial_number"
        >
          <el-input v-model="form.serial_number"></el-input>
        </el-form-item>
        <el-form-item
          label="设备名称"
          :label-width="formLabelWidth"
          prop="name"
        >
          <el-input v-model="form.name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="parseInt(resData.page)"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="parseInt(resData.pageSize)"
        layout="total, sizes, prev, pager, next, jumper"
        :total="parseInt(resData.total)"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { get, postJSON, del } from "@/utils/request.js";
export default {
  data() {
    return {
      resData: {},
      params: {
        page: 1,
        pageSize: 5,
      },
      dialogFormVisible: false,
      form: {
        name: "",
        serial_number: "",
      },
      formLabelWidth: "120px",
      rules: {
        name: [{ required: true, message: "请输入设备名称", trigger: "blur" }],
        serial_number: [
          { required: true, message: "请输入设备编号", trigger: "blur" },
        ],
      },
      title: "",
      engineer: [],
    };
  },
  created() {
    this.pageQueryDevices();
    this.getAllEngineer();
  },
  methods: {
    //事件监听
    handleSizeChange(pageSize) {
      console.log(pageSize);
      this.params.pageSize = pageSize;
      this.pageQueryDevices();
    },
    //page改变事件
    handleCurrentChange(page) {
      this.params.page = page;
      this.pageQueryDevices();
    },
    toAddClick() {
      this.title = "添加设备";
      this.form = {};
      this.dialogFormVisible = true;
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.$refs["ruleForm"].resetFields();
    },
    submitForm() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          postJSON("/device/saveOrUpdate", this.form).then((res) => {
            this.pageQueryDevices();
            this.$message({
              message: "操作成功",
              type: "success",
            });
            this.dialogFormVisible = false;
          });
        } else {
          this.$message({
            message: "表单验证失败",
            type: "error",
          });
        }
      });
    },
    toEdit(row) {
      this.form = {
        id: row.id,
        name: row.name,
        serial_number: row.serial_number,
      };
      this.title = "修改设备";
      this.dialogFormVisible = true;
    },
    toSubmit() {
      this.pageQueryDevices();
      console.log(this.engineer);
    },
    toRemove(id) {
      this.$confirm("此操作将永久删除该设备, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        del("/device/deleteById", { id }).then(() => {
          this.pageQueryDevices();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        });
      });
    },
    //网络请求
    pageQueryDevices() {
      get("/device/pageQuery", this.params).then((res) => {
        this.resData = res.data;
      });
    },
    getAllEngineer() {
      get("/engineer/pageQuery", {
        page: 1,
        pageSize: 100,
      }).then((res) => {
        if (res) {
          this.engineer = res.data.list;
        }
      });
    },
  },
};
</script>

<style>
</style>